/***********************************************
 This file is part of redia-lib-utils-ios
.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#pragma once

#ifndef DLog
#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif
#endif

// ALog always displays output regardless of the DEBUG setting
#ifndef ALog
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#endif

#define EXPECT_OBJECT(classname, variable) \
    if (variable == nil || ![variable isKindOfClass:[classname class]]) {  \
        NSLog(@"ERROR: received bad data type"); \
        [NSException raise:@"Unexpected data structure received from RPC" format:@"Expected %s, encountered %@ for var %s",#classname, variable, #variable]; \
    }

#define EXPECT_OBJECT_ELSE_RETURN_NIL(classname, variable) \
    if (variable == nil || ![variable isKindOfClass:[classname class]]) {  \
        NSLog(@"ERROR: received bad data type, expected %s, encountered %@ for var %s",#classname, variable, #variable); \
        return nil; \
    }

#ifndef SINGLETON_DECL_GCD
#define SINGLETON_DECL_GCD(classname)                       \
+ (classname *)instance;
#endif

#ifndef SINGLETON_IMPL_GCD
#define SINGLETON_IMPL_GCD(classname)                       \
                                                            \
+ (classname *)instance {                                   \
                                                            \
    static dispatch_once_t pred;                            \
    __strong static classname * shared##classname = nil;    \
    dispatch_once( &pred, ^{                                \
        shared##classname = [[self alloc] init]; });        \
    return shared##classname;                               \
}                                                           
#endif

#ifndef SINGLETON_VIEWCONTROLLER_IMPL_GCD
#define SINGLETON_VIEWCONTROLLER_IMPL_GCD(classname)       \
\
+ (classname *)instance {                                   \
\
static dispatch_once_t pred;                            \
__strong static classname * shared##classname = nil;    \
dispatch_once( &pred, ^{                                \
shared##classname = [[self alloc] initWithNibName:nil bundle:nil]; });        \
return shared##classname;                               \
}          
#endif

#ifndef RD_IS_RETINA_SCREEN
#define RD_IS_RETINA_SCREEN ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))
#endif

#define RD_SET_EDGES_FOR_EXTENDED_LAYOUT_IOS7                                   \
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {      \
        [self setEdgesForExtendedLayout:UIRectEdgeNone];                        \
    }


#define RD_IS_IPAD ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define RD_IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

#define RD_IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)


#define ColorFromRGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

